Socket
Socket
Sign inDemoInstall

cls-hooked

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cls-hooked

CLS using AsynWrap instead of async-listener - Node >= 4.7.0


Version published
Weekly downloads
1.7M
decreased by-0.47%
Maintainers
1
Weekly downloads
 
Created

What is cls-hooked?

The cls-hooked package is a Node.js module that allows you to create namespace-like contexts that persist across asynchronous operations. It uses the async_hooks module to track the context of asynchronous calls, enabling you to store and access data throughout the lifecycle of a request or any other asynchronous execution flow. This is particularly useful for logging, tracing, and passing request-specific data without the need to explicitly pass parameters through each function call.

What are cls-hooked's main functionalities?

Creating and using a namespace

This feature allows you to create a namespace and use it to set and get context values across asynchronous boundaries.

const createNamespace = require('cls-hooked').createNamespace;
const namespace = createNamespace('myNamespace');

namespace.run(() => {
  namespace.set('key', 'value');
  asyncFunction();
});

function asyncFunction() {
  process.nextTick(() => {
    console.log(namespace.get('key')); // Outputs: 'value'
  });
}

Maintaining context in async/await

This feature demonstrates how cls-hooked can be used with async/await to maintain context across asynchronous calls.

const createNamespace = require('cls-hooked').createNamespace;
const namespace = createNamespace('myNamespace');

async function main() {
  namespace.runPromise(async () => {
    namespace.set('key', 'value');
    await asyncFunction();
  });
}

async function asyncFunction() {
  await new Promise(resolve => setTimeout(resolve, 100));
  console.log(namespace.get('key')); // Outputs: 'value'
}

main();

Integrating with Express middleware

This feature shows how cls-hooked can be integrated with Express middleware to maintain request-specific data across middleware and route handlers.

const createNamespace = require('cls-hooked').createNamespace;
const namespace = createNamespace('myNamespace');
const express = require('express');
const app = express();

app.use((req, res, next) => {
  namespace.run(() => {
    namespace.set('key', req.query.key);
    next();
  });
});

app.get('/', (req, res) => {
  res.send(`Key: ${namespace.get('key')}`);
});

app.listen(3000);

Other packages similar to cls-hooked

Keywords

FAQs

Package last updated on 26 Jul 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc